# coding:utf-8
from .data_collector import DataCollector
from bs4 import BeautifulSoup
import pandas as pd
from urllib.request import urlretrieve


class DoubanCrawler(DataCollector):
    movie_limit_count = 25
    movie_items = []
    movie_list_url = "https://movie.douban.com/top250?start=0"

    def __init__(self):
        DataCollector.__init__(self)
        self.algorithm_name = "豆瓣电影"
        self.ipynb_template_name = "douban_crawler-template.ipynb"

    def implent(self):
        DataCollector.implent(self)
        self.movie_items = []
        page_url = self.movie_list_url
        while True:
            result = self.http.request(method="GET", url=page_url)
            soup = BeautifulSoup(result.data.decode(), "html.parser")
            for item in soup.find_all("div", attrs={"class": "item"}):
                pic_url = item.find(
                    "div", attrs={"class": "pic"}).find("img")["src"]
                movie_url = item.find(
                    "div", attrs={"class": "pic"}).find("a")["href"]
                title = item.find("div", attrs={"class": "info"}).find(
                    "span", attrs={"class": "title"}).text
                rating_num = float(
                    item.find("span", attrs={"class": "rating_num"}).text)
                if item.find("p", attrs={"class": "quote"}):
                    remark = item.find("p", attrs={"class": "quote"}).text
                self.movie_items.append(
                    [title, rating_num, remark, movie_url, pic_url])
            if len(self.movie_items) > self.movie_limit_count:
                break
            else:
                find_next_page = False
                if soup.find("span", attrs={"class": "next"}).find("a"):
                    page_url = "https://movie.douban.com/top250" + \
                        soup.find("span", attrs={"class": "next"}).find(
                            "a")["href"]
                    find_next_page = True
                if not find_next_page:
                    break
        self.movie_items = self.movie_items[:self.movie_limit_count]
        self.df = pd.DataFrame(self.movie_items, columns=[
                               "电影标题", "豆瓣评分", "一句话影评", "电影详情链接", "图片URL地址"])
        # 下载图片
        for i in range(len(self.movie_items)):
            urlretrieve(url=self.movie_items[i][4], filename="%s/%d.png" % (self.chart_path, i + 1))

    def prepareIpynbItems(self):
        DataCollector.prepareIpynbItems(self)
        self.ipynb_items["#movie_list_url#"] = self.movie_list_url
        self.ipynb_items["#movie_limit_count#"] = self.movie_limit_count
